<template>
  <div class="comment-item">
    <!-- 一级评论 -->
    <div class="main-replay">
      <el-avatar :size="45"
                 :src="commentobj.author.avatar">
      </el-avatar>
      <div class="comment-write">
        <div class="comment-username">{{commentobj.author.nickname}}</div>
        <div class="comment-content">{{commentobj.content}}</div>
      </div>
    </div>
    <!-- 回复按钮 -->
    <div class="rep-btn">
      <span @click="replay(articleId,commentobj)">回复</span>
    </div>

    <div class="second-reply-list">
      <!-- 二级评论 -->
      <div class="reply-item"
           v-for="seccom in commentobj.children"
           :key="seccom.id">
        <div>
          <span style="padding-right:5px;color:#33c5c5">{{seccom.author.nickname}}</span>
          <span>回复&nbsp;@{{seccom.toUser.nickname}}</span>
          <span>: {{seccom.content}}</span>
        </div>
        <div class="second-rep-btn">
          <span @click="replay(articleId,seccom)">回复</span>
        </div>

      </div>
      <!-- 输入框 -->
      <div class="reply-ipt"
           v-show="commentShow">
        <el-input v-model="reply.content"
                  type="input"
                  style="width: 90%"
                  :placeholder="placeholder"
                  class="me-view-comment-text"
                  resize="none">
        </el-input>
        <el-button style="margin-left: 8px"
                   @click="publishComment"
                   type="text">发布</el-button>
      </div>
    </div>

  </div>
</template>

<script>
import defaultavator from '@/assets/images/default-avator.png'
import { pubComment } from '@/api/article'
export default {
  props: ['commentobj', 'articleId'],
  data() {
    return {
      defaultavator,
      placeholder: '输入内容',
      commentShow: false,
      reply: this.getEmptyReply()
    }
  },
  methods: {
    //点开输入框
    replay(aid,comment) {
      this.commentShow = !this.commentShow
      this.placeholder = `@ ${comment.author.nickname}`
      this.reply.toUserId = comment.author.id
      this.reply.aid = aid
    },
    // 发布评论
    async publishComment() {
      if (this.reply.content) {
        console.log(this.reply)
        let { data: res } = await pubComment(this.reply)
        this.reply.content = ''
        this.commentShow = false
        this.$emit('getNewComments')
      }
    },
    getEmptyReply() {
      return {
        aid: '',
        pid: this.commentobj.id,
        toUserId: '',
        content: ''
      }
    }
  }
}
</script>

<style lang="less" scoped>
.comment-item {
  margin: 25px 0;
  padding-left: 20px;
  /* 一级评论 */
  .main-replay {
    display: flex;
    align-items: center;
    .el-avatar {
      cursor: pointer;
    }
    /* 评论文字 */
    .comment-write {
      margin-left: 10px;
      flex: 1;
      .comment-username {
        font-weight: 600;
        color: #33c5c5;
      }
      .comment-content {
        font-size: 14px;
      }
    }
  }
  /* 回复按钮 */
  .rep-btn {
    height: 15px;
    text-align: right;
    font-size: 14px;
    span {
      color: #33c5c5;
      cursor: pointer;
    }
  }
  /* 回复输入框 */
  ::v-deep .reply-ipt {
    margin-top: 5px;
    .el-input__inner {
      outline: none;
      height: 35px;
      &:focus {
        border: 1px solid #33c5c5;
      }
    }
  }
  /* 二级评论 */
  .second-reply-list {
    margin: 7px;
    padding-left: 15px;
    border-left: 2px solid #33c5c5;
    .reply-item {
      font-size: 14px;
      /* 二级评论按钮 */
      .second-rep-btn {
        margin-top: 4px;
        span {
          color: #33c5c5;
          cursor: pointer;
          font-weight: 600;
        }
      }
    }
  }
}
</style>